package com.czkevin.guicontrolsapi.controls;

import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import com.czkevin.guicontrolsapi.ItemUtils;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class ButtonControl extends AbstractSingleItemControl {

    private static final ItemStack DEFAULT_ICON = new ItemStack(Material.LEVER);

    private ItemStack item;
    private BiConsumer<HumanEntity, ClickType> handler = null;
    private final String label;
    private final String descriptions;

    public ButtonControl(Consumer<HumanEntity> handler, String label, String descriptions) {
        this(null, handler != null ? (h, t) -> handler.accept(h) : null, label, descriptions);
    }

    public ButtonControl(BiConsumer<HumanEntity, ClickType> handler, String label, String descriptions) {
        this(null, handler, label, descriptions);
    }

    public ButtonControl(ItemStack icon, Consumer<HumanEntity> handler, String label, String descriptions) {
        this(icon, handler != null ? (h, t) -> handler.accept(h) : null, label, descriptions);
    }

    public ButtonControl(ItemStack icon, BiConsumer<HumanEntity, ClickType> handler, String label, String descriptions) {
        if(icon == null) icon = DEFAULT_ICON.clone();
        this.item = icon.clone();
        this.handler = handler;
        this.label = label;
        this.descriptions = descriptions;
    }

    public ButtonControl(ItemStack icon, String label, String descriptions) {
        if(icon == null) icon = DEFAULT_ICON.clone();
        this.item = icon.clone();
        this.label = label;
        this.descriptions = descriptions;
    }

    public void setHandler(Consumer<HumanEntity> handler) {
        if (handler == null) {
            this.handler = null;
        } else {
            this.handler = (h, t) -> handler.accept(h);
        }
    }

    public void setHandler(BiConsumer<HumanEntity, ClickType> handler) {
        this.handler = handler;
    }

    @Override
    public void onClick(InventoryClickEvent event, int localX, int localY) {
        event.setCancelled(true);
        if(event.getClick() == ClickType.DOUBLE_CLICK) return;
        handler.accept(event.getWhoClicked(), event.getClick());
    }

    @Override
    public int getWidth() {
        return 1;
    }

    @Override
    public int getHeight() {
        return 1;
    }

    @Override
    protected ItemStack createDisplay() {
        String[] splitted = descriptions.split("\n");
        String[] out = new String[splitted.length];
        for(int i = 0; i < splitted.length; i++) {
            if(splitted[i].startsWith("\u00a7")) {
                out[i] = splitted[i];
            } else {
                out[i] = "\u00a7f" + splitted[i];
            }
        }
        ItemUtils.putMeta(item, false, "\u00a77[\u00a72※\u00a77] " + (label.startsWith("\u00a7") ? "" : "\u00a7f\u00a7l") + label, out);
        return item;
    }
}
